package com.aviation.sell_ticket.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.aviation.sell_ticket.mapper.AirportMapper;
import com.aviation.sell_ticket.pojo.Aviation_airport;
import com.aviation.sell_ticket.pojo.Aviation_city;
import com.aviation.sell_ticket.service.AirportService;
import com.aviation.sell_ticket.tool.RedisUtil;
import com.aviation.sell_ticket.tool.ResultEnum;
import com.aviation.sell_ticket.tool.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class AirportServiceImpl implements AirportService {

    @Resource
    private AirportMapper airportMapper;//引入AirportMapper

    @Resource
    private RedisUtil redis;//引入redis


    /**
     * 根据城市id查询机场id
     *
     * @param cityId 城市id
     * @return List<Aviation_airport>
     */
    @Override
    public List<Aviation_airport> obtainAirport(Integer cityId) {
        return airportMapper.obtainAirport(cityId);
    }

    /**
     * 查询中间机场
     *
     * @param airportId 机场id
     * @return List<Aviation_airport>
     */
    @Override
    public List<Aviation_airport> middleAirportQuery(Integer airportId) {
        return airportMapper.middleAirportQuery(airportId);
    }

    //获取全部机场信息
    @Override
    public List<Aviation_airport> airportWhole() {
        JSONArray airportArray = null;
        List<Aviation_airport> airportList=null;
        //从缓存中提取数据
        String airportData = redis.getRedis("airport");
        //判断缓存是否存在
        if (null == airportData) {
            //从数据库提取数据
            airportList = airportMapper.airportWhole();
            //转换json
            airportArray = JSONArray.parseArray(JSON.toJSONString(airportList));
            //添加数据进缓存
            redis.setRidesTime("airport", airportArray.toJSONString(), (long) (60 * 60), TimeUnit.SECONDS);
        } else {
            //缓存中有数据，转成List
            airportList = JSONArray.parseArray(airportData, Aviation_airport.class);
        }
        return airportList;
    }
}
